package com.dykj.apps.web.sysmanager.action;

import java.util.List;
import java.util.Map;

import com.dykj.apps.web.core.BaseAction;
import com.dykj.apps.web.sysmanager.dto.SysRoleDTO;
import com.dykj.apps.web.sysmanager.service.IRoleManagerBS;
import com.dykj.apps.web.sysmanager.service.IUserManagerBS;
import com.dykj.platform.framework.exception.ApplicationException;

public class SysRoleAction extends BaseAction  {

	private static final long serialVersionUID = -1520838522673853369L;
	
	private String radio_id;
	private SysRoleDTO role =new SysRoleDTO();
	private IRoleManagerBS bs;
	private IUserManagerBS userbs;
	private List nocheckUserList;
	private List checkUserList;
	private String userList;
	
	/**
	 * 添加新角色
	 * @return
	 * @throws ApplicationException
	 */
	public String initAddRole()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String type = info.get("type").toString();
		if("0".equals(type)){
		nocheckUserList = userbs.queryRoleUser(null, null);	
		}else{
		String groupid=info.get("groupid").toString();
		nocheckUserList = userbs.queryRoleUser(null, groupid);
		}
		this.request("editable", "true");//表单可编辑（默认）
		this.request("action", "roleManager");//action名称
		this.request("bn_value", "新增");//按钮名称
		this.request("bn_method", "saveRole;");//按钮的方法
		return "forwardinitAddrole";
	}
	/**
	 * 保存角色
	 * @return
	 * @throws ApplicationException
	 */
	public String saveRole()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String username=info.get("username").toString();
		if(bs.queryBydesc(role.getRoledesc(),role.getRoleid(),username).size()>0){
			alert("角色描述不能重复");
			return initAddRole();
		}else{
			role.setCreator(username);
			bs.saveOrUpdateRole(role,userList.split(";"));
			role.init();
			alert("保存成功！"); //提示信息
		}
		return queryForPage();
	}
	
	/**
	 * 查看角色信息
	 * @return
	 */
	public String viewRole()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String type = info.get("type").toString();
		if("0".equals(type)){
			nocheckUserList = userbs.queryRoleUser(null, null);	
			checkUserList = userbs.queryRoleUser(Long.valueOf(radio_id), null);
			}else{
			String groupid=info.get("groupid").toString();
			nocheckUserList = userbs.queryRoleUser(null, groupid);
			checkUserList = userbs.queryRoleUser(Long.valueOf(radio_id),groupid);
			}		
		SysRoleDTO roledto = bs.getOneSysRole(Long.parseLong(radio_id));
		roledto.setP_roledesc(role.getP_roledesc());
		roledto.setP_roleid(role.getP_roleid());
		role = roledto;
		this.request("editable", "false");//表单是不可编辑（默认）
		this.request("action", "roleManager");//action名称
		this.request("bn_value", "修改;返回");//按钮名称
		this.request("bn_method", "initUpdateRole;queryForPage");//按钮的方法
		return "forwardinitAddrole";
	}
	/**
	 * 初始化修改角色
	 * @return
	 */
	public String initUpdateRole() throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String type = info.get("type").toString();
		if("0".equals(type)){
			nocheckUserList = userbs.queryRoleUser(null, null);	
			checkUserList = userbs.queryRoleUser(Long.valueOf(radio_id), null);
			}else{
			String groupid=info.get("groupid").toString();
			nocheckUserList = userbs.queryRoleUser(null, groupid);
			checkUserList = userbs.queryRoleUser(Long.valueOf(radio_id),groupid);
			}		
		SysRoleDTO roledto = bs.getOneSysRole(Long.parseLong(radio_id));
		roledto.setP_roledesc(role.getP_roledesc());
		roledto.setP_roleid(role.getP_roleid());
		role = roledto;
		this.request("editable", "true");//表单可编辑（默认）
		this.request("action", "roleManager");//action名称
		this.request("bn_value", "修改;返回");//按钮名称
		this.request("bn_method", "updateRole;queryForPage");//按钮的方法
		return "forwardinitAddrole";
	}
	/**
	 * 保存修改角色
	 * @return
	 */
	public String updateRole()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String username=info.get("username").toString();
		if(bs.queryBydesc(role.getRoledesc(),role.getRoleid(),username).size()>0){
			alert("角色描述不能重复");
			return initUpdateRole();
		}else{
		bs.saveOrUpdateRole(role,userList.split(";"));}
		String type = info.get("type").toString();
		if("0".equals(type)){
			nocheckUserList = userbs.queryRoleUser(null, null);	
			checkUserList = userbs.queryRoleUser(Long.valueOf(radio_id), null);
			}else{
			String groupid=info.get("groupid").toString();
			nocheckUserList = userbs.queryRoleUser(null, groupid);
			checkUserList = userbs.queryRoleUser(Long.valueOf(radio_id),groupid);
			}		
		alert("保存成功！");
		this.request("editable", "true");//表单可编辑（默认）
		this.request("action", "roleManager");//action名称
		this.request("bn_value", "修改;返回");//按钮名称
		this.request("bn_method", "updateRole;queryForPage");//按钮的方法
		return "forwardinitAddrole";
	}
	/**
	 * 删除角色
	 * @return
	 */
	public String deleteRole()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String type = info.get("type").toString();
		int i;
		if("0".equals(type)){
			i = bs.delRole(Long.parseLong(radio_id),null);
		}else{
			String groupid=info.get("groupid").toString();
			i = bs.delRole(Long.parseLong(radio_id),groupid);
			}
		if(i<0){
			alert("还有用户拥用此角色，不能删除");
		}else{
			alert("删除成功！");
		}
		return queryForPage();
	}
	/**
	 * 初始化分页信息
	 * @return
	 */
	public String initQuerySole()throws ApplicationException{
		this.completeResultList(new SysRoleDTO());
		this.request("action", "roleManager");//action名称
		this.request("bn_value", "添加新角色");//按钮名称
		this.request("bn_method", "initAddRole");//按钮的方法
		return "forwardRoleManager";
	}
	
	/**
	 * 初始化分页信息
	 * @return
	 */
	public String queryForPage() throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String username=info.get("username").toString();
		role.setCreator(username);
		this.setPageList(bs.querySysRole(role, this.getPage())); //把查询的结果放入LIST
		this.completeResultList(new SysRoleDTO());//条数不够补齐
		this.request("pageMethod", "queryForPage");
		this.request("action", "roleManager");//action名称
		this.request("bn_value", "查看;修改;删除;添加新角色");//按钮名称
		this.request("bn_method", "viewRole;initUpdateRole;deleteRole;initAddRole");//按钮的方法
		this.request("bn_check", "true;true;true;false");//提交时是否验证单选按钮
		return "forwardRoleManager";
	}
	/*
	 * ================================get set方法=====================================
	 */	
	public IRoleManagerBS getBs() {
		return bs;
	}
	public void setBs(IRoleManagerBS bs) {
		this.bs = bs;
	}
	public String getRadio_id() {
		return radio_id;
	}
	public void setRadio_id(String radio_id) {
		this.radio_id = radio_id;
	}
	public SysRoleDTO getRole() {
		return role;
	}
	public void setRole(SysRoleDTO role) {
		this.role = role;
	}
	public Object getModel() {
		return role;
	}
	public List getCheckUserList() {
		return checkUserList;
	}
	public void setCheckUserList(List checkUserList) {
		this.checkUserList = checkUserList;
	}
	public List getNocheckUserList() {
		return nocheckUserList;
	}
	public void setNocheckUserList(List nocheckUserList) {
		this.nocheckUserList = nocheckUserList;
	}

	public String getUserList() {
		return userList;
	}
	public void setUserList(String userList) {
		this.userList = userList;
	}
	public IUserManagerBS getUserbs() {
		return userbs;
	}
	public void setUserbs(IUserManagerBS userbs) {
		this.userbs = userbs;
	}
	
}
